package com.tt.jshb.bill.zq;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzecool.core.base.TBaseActivity;
import com.hzecool.core.rxbus.RxBus;
import com.hzecool.widget.materialdialog.MaterialDialog;
import com.tt.jshb.R;
import com.tt.jshb.bean.FinishEvent;
import com.tt.jshb.bean.PayBean;
import com.tt.jshb.bean.YQBean;
import com.tt.jshb.bill.pay.QrCodePayActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by tu on 2018/11/28.
 */
public class YQActivity extends TBaseActivity<YQView, YQPresenter>
        implements YQView {
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.ll_qx)
    LinearLayout llQx;
    @BindView(R.id.tv_fy)
    TextView tvFy;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private YQBean yqBean;
    private String ts;

    @Override
    public void onLoadData(Object o) {

    }

    @Override
    public void onEmptyData() {

    }

    @Override
    public void onLoadError(String msg) {

    }

    @Override
    public void onNetError(String msg) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.yq_activity_layout;
    }

    @Override
    public void initView() {
        String id = getIntent().getStringExtra("id");
        mPresenter.getData(id);


        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                List<String> items = new ArrayList<>();
                items.add("支付宝");
                items.add("微信");

                new MaterialDialog.Builder(YQActivity.this)
                        .title("选择展期期限")
                        .items(items)
                        .itemsCallbackSingleChoice(-1, (dialog, view, which, text) -> {
                            String fs = which == 0 ? "1" : "3";
                            mPresenter.payInfo(id, ts, fs);
                            return true;
                        })
                        .positiveText("确定")
                        .negativeText("取消")
                        .cancelable(false)
                        .show();
            }
        });

        RxBus.obtainEvent(FinishEvent.class)
                .subscribe(new Action1<FinishEvent>() {
                    @Override
                    public void call(FinishEvent finishEvent) {
                        if (finishEvent.getType()==1){
                            finish();
                        }
                    }
                });
    }

    @Override
    public void initTitle(ImageView ivBack, TextView tvBack, View llBack, TextView titleName, TextView tvMenu, View titleRoot) {
        titleName.setText("确认展期");
    }

    @Override
    protected YQPresenter createPresenter() {
        return new YQPresenter();
    }

    @Override
    public void onLoadList(YQBean bean) {
        yqBean = bean;
        ts = yqBean.getData().getXq().get(0).getTime();
        tvQx.setText(yqBean.getData().getXq().get(0).getTime() + "天");
        tvFy.setText(yqBean.getData().getXq().get(0).getPrice() + "元");

        llQx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> items = new ArrayList<>();

                for (YQBean.DataBean.XqBean xqBean : bean.getData().getXq()) {
                    items.add(xqBean.getTime());
                }

                new MaterialDialog.Builder(YQActivity.this)
                        .title("选择展期期限")
                        .items(items)
                        .itemsCallbackSingleChoice(-1, (dialog, view, which, text) -> {
                            ts = text.toString();
                            tvQx.setText(text + "天");
                            tvFy.setText(yqBean.getData().getXq().get(which).getPrice() + "元");
                            return true;
                        })
                        .positiveText("确定")
                        .show();
            }
        });
    }

    @Override
    public void onPayResult(PayBean bean) {
        Intent intent = new Intent(this,QrCodePayActivity.class);
        intent.putExtra("payInfo",bean);
        startActivity(intent);
    }
}
